package ecodex.serviciospruebas.frames;

import java.io.*;
import javax.swing.Icon;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import ecodex.serviciospruebas.Utils;
import ecodex.serviciospruebas.Servicios;
import ecodex.restapi.*;
import ecodex.restapi.models.*;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;

/**
 *
 * @author Ecodex6
 */
public class JFrameServicios extends javax.swing.JFrame {
    public static String RFC_Emisor_Pruebas;
    public static String RFC_Integrador_Pruebas;
    private String filePath = new String();

    /**
     * Creates new form JFrameTimbrado
     */
    public JFrameServicios() {
        initComponents();
        //Utils Utilidades = new Utils();
        RFC_Integrador_Pruebas = Utils.getPropertyValue("RFC_INTEGRADOR_PRUEBAS");
        RFC_Emisor_Pruebas = Utils.getPropertyValue("RFC_EMISOR_PRUEBAS");
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnTimbrarXML = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnEstatus = new javax.swing.JButton();
        btnObtenerComprobante = new javax.swing.JButton();
        btnSeleccionarDirectorio = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListArchivos = new javax.swing.JList();
        canvas1 = new java.awt.Canvas();
        canvas2 = new java.awt.Canvas();
        QRCode = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtTimbra = new javax.swing.JTextPane();
        imgQR = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtUUID = new javax.swing.JTextField();
        btnCancelarOtros = new javax.swing.JButton();
        btnRecuperarAcuse = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();
        btnAltaEmisor = new javax.swing.JButton();
        btnAsignarTimbres = new javax.swing.JButton();
        btnEstatusEmisor = new javax.swing.JButton();
        btnObtenerPorHash = new javax.swing.JButton();
        btnClaveCarga = new javax.swing.JButton();
        btnCancelarTimbrado = new javax.swing.JButton();
        btnObtenerUltimos = new javax.swing.JButton();
        btnTimbrarCfdiTest = new javax.swing.JButton();
        btnTimbrarRetencionTest = new javax.swing.JButton();
        btnObtenerToken = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Timbrado");
        setResizable(false);

        btnTimbrarXML.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnTimbrarXML.setText("Timbrar XML");
        btnTimbrarXML.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimbrarXMLActionPerformed(evt);
            }
        });

        btnCancelar.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnCancelar.setText("Cancelación Múltiple");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnEstatus.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnEstatus.setText("Estatus Comprobante");
        btnEstatus.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEstatusActionPerformed(evt);
            }
        });

        btnObtenerComprobante.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnObtenerComprobante.setText("Obtener Comprobante");
        btnObtenerComprobante.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnObtenerComprobanteActionPerformed(evt);
            }
        });

        btnSeleccionarDirectorio.setText("Selecciona directorio XML");
        btnSeleccionarDirectorio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarDirectorioActionPerformed(evt);
            }
        });

        jScrollPane1.setViewportView(ListArchivos);

        QRCode.setText("Codigo QR:");
        QRCode.setToolTipText("");

        jScrollPane2.setViewportView(txtTimbra);

        imgQR.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        jLabel1.setText("Comprobante XML/ Mensaje:");

        jLabel2.setText("UUID SAT:");

        btnCancelarOtros.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnCancelarOtros.setText("Cancelar Otros");
        btnCancelarOtros.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarOtrosActionPerformed(evt);
            }
        });

        btnRecuperarAcuse.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnRecuperarAcuse.setText("Recuperar Acuse");
        btnRecuperarAcuse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRecuperarAcuseActionPerformed(evt);
            }
        });

        btnSalir.setText("Salir");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        btnAltaEmisor.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnAltaEmisor.setText("Alta Emisor");
        btnAltaEmisor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAltaEmisorActionPerformed(evt);
            }
        });

        btnAsignarTimbres.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnAsignarTimbres.setText("Asignar \nTimbres");
        btnAsignarTimbres.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        btnAsignarTimbres.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnAsignarTimbres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAsignarTimbresActionPerformed(evt);
            }
        });

        btnEstatusEmisor.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnEstatusEmisor.setText("Estatus Emisor");
        btnEstatusEmisor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEstatusEmisorActionPerformed(evt);
            }
        });

        btnObtenerPorHash.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnObtenerPorHash.setText("Obtener por Hash");
        btnObtenerPorHash.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnObtenerPorHashActionPerformed(evt);
            }
        });

        btnClaveCarga.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnClaveCarga.setLabel("Clave carga certificado");
        btnClaveCarga.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClaveCargaActionPerformed(evt);
            }
        });

        btnCancelarTimbrado.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnCancelarTimbrado.setText("Cancelar timbrado");
        btnCancelarTimbrado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarTimbradoActionPerformed(evt);
            }
        });

        btnObtenerUltimos.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnObtenerUltimos.setText("Obtener recientes");
        btnObtenerUltimos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnObtenerUltimosActionPerformed(evt);
            }
        });

        btnTimbrarCfdiTest.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnTimbrarCfdiTest.setText("Timbrar CFDI Test");
        btnTimbrarCfdiTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimbrarCfdiTestActionPerformed(evt);
            }
        });

        btnTimbrarRetencionTest.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnTimbrarRetencionTest.setText("Timbrar Retención Test");
        btnTimbrarRetencionTest.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTimbrarRetencionTestActionPerformed(evt);
            }
        });

        btnObtenerToken.setFont(new java.awt.Font("Tahoma", 0, 10)); // NOI18N
        btnObtenerToken.setText("Obtener Token/Hora");
        btnObtenerToken.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnObtenerTokenActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnSeleccionarDirectorio)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 179, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2))
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addComponent(jScrollPane2))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(QRCode, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(50, 50, 50))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addComponent(imgQR, javax.swing.GroupLayout.PREFERRED_SIZE, 96, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addContainerGap())))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtUUID, javax.swing.GroupLayout.PREFERRED_SIZE, 396, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(btnTimbrarXML, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnObtenerComprobante, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnCancelar, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnAltaEmisor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(btnEstatusEmisor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnCancelarTimbrado, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                                    .addComponent(btnObtenerPorHash, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btnTimbrarCfdiTest, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnAsignarTimbres, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnClaveCarga, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnCancelarOtros, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnRecuperarAcuse, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnEstatus, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnObtenerUltimos, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(btnTimbrarRetencionTest, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(btnObtenerToken, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE)))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnSeleccionarDirectorio)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtUUID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(21, 21, 21))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 224, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 24, Short.MAX_VALUE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnTimbrarXML)
                            .addComponent(btnTimbrarCfdiTest)
                            .addComponent(btnTimbrarRetencionTest)
                            .addComponent(btnObtenerToken))
                        .addGap(11, 11, 11))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addComponent(QRCode, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(imgQR, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(btnObtenerComprobante)
                                .addComponent(btnObtenerPorHash)
                                .addComponent(btnObtenerUltimos))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(btnEstatus)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnCancelar)
                                    .addComponent(btnCancelarTimbrado)
                                    .addComponent(btnCancelarOtros)
                                    .addComponent(btnRecuperarAcuse))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(btnAltaEmisor)
                                    .addComponent(btnEstatusEmisor)
                                    .addComponent(btnAsignarTimbres, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btnClaveCarga))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                        .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalir, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );

        btnEstatusEmisor.getAccessibleContext().setAccessibleName("");
        btnClaveCarga.getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnTimbrarXMLActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimbrarXMLActionPerformed

        String xmlFileName = (String) ListArchivos.getSelectedValue();

        if (xmlFileName != null) {
            try {
                String xmlFullFileName = filePath + File.separator + xmlFileName;
                Servicios servicios = new Servicios();
                String comprobanteTimbrado = servicios.timbrarComprobante(RFC_Emisor_Pruebas, xmlFullFileName);
                String uuid = Utils.searchAttributeintoXML(comprobanteTimbrado, "UUID");
                Icon qr = servicios.obtenerQRComprobante(RFC_Emisor_Pruebas, uuid);
                txtTimbra.setText(comprobanteTimbrado);
                txtUUID.setText(uuid);
                imgQR.setIcon(qr);
            } catch (IOException ex) {
                txtTimbra.setText(ex.toString());
            } catch (ParserConfigurationException | 
                    SAXException | 
                    XPathExpressionException ex) {
                Logger.getLogger(JFrameServicios.class.getName()).log(Level.SEVERE, null, ex);
            }
        } else {
            txtTimbra.setText("No se seleccionó ningún archivo");
        }
    }//GEN-LAST:event_btnTimbrarXMLActionPerformed

    @SuppressWarnings("unchecked")
    private void btnSeleccionarDirectorioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarDirectorioActionPerformed

        try {
            JFileChooser fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int returnVal = fc.showDialog(JFrameServicios.this, "Seleccionar Directorio");
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                filePath = fc.getSelectedFile().getPath();
                File dir = new File(filePath);
                String[] archivos = dir.list();
                if (archivos.length > 0) {
                    ListArchivos.setListData(archivos);
                }
            }
        } catch (Exception ex) {
            txtTimbra.setText(ex.toString());
        }
    }//GEN-LAST:event_btnSeleccionarDirectorioActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed

        JFrameComprobanteCancela frm = new JFrameComprobanteCancela();
        frm.setVisible(true);
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnEstatusActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEstatusActionPerformed

        JFrameComprobanteEstatus frm = new JFrameComprobanteEstatus();
        frm.setVisible(true);
    }//GEN-LAST:event_btnEstatusActionPerformed

    private void btnObtenerComprobanteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnObtenerComprobanteActionPerformed

        JFrameComprobanteRecuperar frm = new JFrameComprobanteRecuperar();
        frm.setVisible(true);
    }//GEN-LAST:event_btnObtenerComprobanteActionPerformed

    private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
        System.exit(0);
    }//GEN-LAST:event_btnSalirActionPerformed

    private void btnRecuperarAcuseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRecuperarAcuseActionPerformed
        JFrameComprobanteRecuperarAcuse frm = new JFrameComprobanteRecuperarAcuse();
        frm.setVisible(true);
    }//GEN-LAST:event_btnRecuperarAcuseActionPerformed

    private void btnCancelarOtrosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarOtrosActionPerformed
        JFrameComprobanteCancelaOtros frm = new JFrameComprobanteCancelaOtros();
        frm.setVisible(true);
    }//GEN-LAST:event_btnCancelarOtrosActionPerformed

    private void btnAltaEmisorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAltaEmisorActionPerformed
        JFrameEmisorAlta frm = new JFrameEmisorAlta();
        frm.setVisible(true);
    }//GEN-LAST:event_btnAltaEmisorActionPerformed

    private void btnEstatusEmisorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEstatusEmisorActionPerformed
        JFrameEmisorEstatus frm = new JFrameEmisorEstatus();
        frm.setVisible(true);
    }//GEN-LAST:event_btnEstatusEmisorActionPerformed

    private void btnAsignarTimbresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAsignarTimbresActionPerformed
        JFrameEmisorAsignarTimbres frm = new JFrameEmisorAsignarTimbres();
        frm.setVisible(true);
    }//GEN-LAST:event_btnAsignarTimbresActionPerformed

    private void btnObtenerPorHashActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnObtenerPorHashActionPerformed
        JFrameComprobantePorHash frm = new JFrameComprobantePorHash();
        frm.setVisible(true);
    }//GEN-LAST:event_btnObtenerPorHashActionPerformed

    private void btnCancelarTimbradoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarTimbradoActionPerformed
        JFrameComprobanteCancelaTimbrado frm = new JFrameComprobanteCancelaTimbrado();
        frm.setVisible(true);
    }//GEN-LAST:event_btnCancelarTimbradoActionPerformed

    private void btnObtenerUltimosActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnObtenerUltimosActionPerformed
        JFrameComprobantesRecientes frm = new JFrameComprobantesRecientes();
        frm.setVisible(true);
    }//GEN-LAST:event_btnObtenerUltimosActionPerformed

    private void btnTimbrarCfdiTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimbrarCfdiTestActionPerformed

        try {
            String xmlFullFileName = System.getProperty("user.dir") + "\\Recursos\\ComprobanteV32.xml";
            Servicios servicios = new Servicios();
            String comprobanteTimbrado = servicios.timbrarComprobante(RFC_Emisor_Pruebas, xmlFullFileName);
            String uuid = Utils.searchAttributeintoXML(comprobanteTimbrado, "UUID");
            Icon qr = servicios.obtenerQRComprobante(RFC_Emisor_Pruebas, uuid);
            txtTimbra.setText(comprobanteTimbrado);
            txtUUID.setText(uuid);
            imgQR.setIcon(qr);
        } catch (IOException ex) {
            txtTimbra.setText(ex.toString());
        } catch (ParserConfigurationException | 
                SAXException | 
                XPathExpressionException ex) {
            Logger.getLogger(JFrameServicios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnTimbrarCfdiTestActionPerformed

    private void btnTimbrarRetencionTestActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTimbrarRetencionTestActionPerformed
        try {
            String xmlFullFileName = System.getProperty("user.dir") + "\\Recursos\\Constancia_Retenciones_1.xml";
            Servicios servicios = new Servicios();
            String comprobanteTimbrado = servicios.timbrarComprobante(RFC_Emisor_Pruebas, xmlFullFileName);
            String uuid = Utils.searchAttributeintoXML(comprobanteTimbrado, "UUID");            
            txtTimbra.setText(comprobanteTimbrado);
            txtUUID.setText(uuid);
            Icon qr = servicios.obtenerQRComprobante(RFC_Emisor_Pruebas, uuid);
            imgQR.setIcon(qr);
        } catch (IOException ex) {
            txtTimbra.setText(ex.toString());
        } catch (ParserConfigurationException | 
                SAXException | 
                XPathExpressionException ex) {
            Logger.getLogger(JFrameServicios.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnTimbrarRetencionTestActionPerformed

    private void btnClaveCargaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClaveCargaActionPerformed
        JFrameCertificadoObtenerClave frm = new JFrameCertificadoObtenerClave();
        frm.setVisible(true);
    }//GEN-LAST:event_btnClaveCargaActionPerformed

    private void btnObtenerTokenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnObtenerTokenActionPerformed
        JFrameTokenObtener frm = new JFrameTokenObtener();
        frm.setVisible(true);
    }//GEN-LAST:event_btnObtenerTokenActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(JFrameServicios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(JFrameServicios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(JFrameServicios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(JFrameServicios.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new JFrameServicios().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList ListArchivos;
    private javax.swing.JLabel QRCode;
    private javax.swing.JButton btnAltaEmisor;
    private javax.swing.JButton btnAsignarTimbres;
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnCancelarOtros;
    private javax.swing.JButton btnCancelarTimbrado;
    private javax.swing.JButton btnClaveCarga;
    private javax.swing.JButton btnEstatus;
    private javax.swing.JButton btnEstatusEmisor;
    private javax.swing.JButton btnObtenerComprobante;
    private javax.swing.JButton btnObtenerPorHash;
    private javax.swing.JButton btnObtenerToken;
    private javax.swing.JButton btnObtenerUltimos;
    private javax.swing.JButton btnRecuperarAcuse;
    private javax.swing.JButton btnSalir;
    private javax.swing.JButton btnSeleccionarDirectorio;
    private javax.swing.JButton btnTimbrarCfdiTest;
    private javax.swing.JButton btnTimbrarRetencionTest;
    private javax.swing.JButton btnTimbrarXML;
    private java.awt.Canvas canvas1;
    private java.awt.Canvas canvas2;
    private javax.swing.JLabel imgQR;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextPane txtTimbra;
    private javax.swing.JTextField txtUUID;
    // End of variables declaration//GEN-END:variables
}
